package com.zkthink.ceres.app.member.controller;

import cn.hutool.core.util.StrUtil;
import com.zkthink.base.R;
import com.zkthink.cache.repository.CacheRepository;
import com.zkthink.ceres.applet.commission.mapper.AppletCommissionMapper;
import com.zkthink.ceres.common.constant.CacheKey;
import com.zkthink.ceres.member.dto.MemberUpdateDTO;
import com.zkthink.ceres.member.vo.ClhMemberVo;
import com.zkthink.ceres.member.vo.MemberUpdateVO;
import com.zkthink.ceres.member.vo.TuiguangVo;
import com.zkthink.ceres.sms.enumeration.TemplateCodeType;
import com.zkthink.context.BaseContextHandler;
import com.zkthink.dozer.DozerUtils;
import com.zkthink.ceres.member.entity.Member;
import com.zkthink.ceres.member.service.MemberService;
import com.zkthink.ceres.member.vo.MemberVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;

import static com.zkthink.ceres.applet.authority.config.AuthorityWebConfiguration.getMid;

/**
 * Created by kellen on 2020/5/4.
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/member")
@Api(value = "member", tags = "会员服务")
@AllArgsConstructor
public class AppMemberController {

  private MemberService memberService;

  private DozerUtils dozerUtils;

  @Resource
  private AppletCommissionMapper appletCommissionMapper;

  @Autowired
  private CacheRepository cacheRepository;

 // private AuthService authService;

    /**
     * 我的推广
     */
    @GetMapping("/tuiguang")
    @ApiOperation(value = "我的推广", notes = "我的推广")
    public R tuiguang() {
      return R.success(memberService.tuiguang());
    }
  /**
   * 我的推广
   */
  @GetMapping("/tuiguangDetails")
  @ApiOperation(value = "推广明细", notes = "推广明细")
  public R tuiguangDetails() {
    return R.success(memberService.tuiguangDetails());
  }
  /**
   * 查询会员信息
   */
  @GetMapping("/info")
  @ApiOperation(value = "会员信息", notes = "会员信息")
  public R<ClhMemberVo> miniApp() {
    //获取登录会员信息
    Long mid = BaseContextHandler.getUserId();
    Member member = memberService.getById(getMid());
    ClhMemberVo vo = dozerUtils.map(member, ClhMemberVo.class);
    if(vo.getVipTime() != null){
      if (vo.getVipTime().isAfter(LocalDateTime.now())) {
        vo.setB(true);
      }
    }
    //累计收入
    BigDecimal leijishouyi = new BigDecimal("0.00");
    BigDecimal directCommission  = appletCommissionMapper.selectDirectCommission(mid);
    leijishouyi = leijishouyi.add(directCommission);
    BigDecimal inDirectCommission  = appletCommissionMapper.selectInDirectCommission(mid);
    leijishouyi = leijishouyi.add(inDirectCommission);
    vo.setRecommend(appletCommissionMapper.selectDirectCommission(mid).add(appletCommissionMapper.selectInDirectCommission(mid)));

    if (vo.getTichneg() != null) {
      vo.setTichneg(vo.getTichneg().add(leijishouyi));
    }else{
      vo.setTichneg(new BigDecimal(0));
    }
    return R.success(vo);
  }

  /**
   * 更新会员信息
   */
  @ApiOperation(value = "更新会员信息", notes = "更新会员信息，目前允许更新昵称、头像")
  @PostMapping("/updateInfo")
  public R updateInfo(@RequestBody MemberUpdateDTO memberUpdateDTO) {
    Long mid = BaseContextHandler.getUserId();
    Member member = new Member();
    member.setNickname(memberUpdateDTO.getNickname());
    member.setHeadimg(memberUpdateDTO.getHeadimg());
    member.setPhone(memberUpdateDTO.getPhone());
    member.setSex(memberUpdateDTO.getSex());
    member.setCity(memberUpdateDTO.getCity());
    member.setId(mid);
    return R.success(memberService.updateById(member));
  }


  @ApiOperation(value = "获取小程序用户详情", notes = "获取小程序用户详情")
  @GetMapping("/getCeresMember")
  public R getCeresMember() {
    return R.success(memberService.findById(getMid()));
  }

  @ApiOperation(value = "修改小程序用户", notes = "修改小程序用户")
  @PostMapping("/updateCeresMember")
  public R updateCeresMember(@RequestBody MemberUpdateVO memberUpdateVO) {
    //短信验证码校验
//    String key = CacheKey
//            .buildKey(CacheKey.ADD_verify, TemplateCodeType.CODE_Add_verify.name(),
//                    memberService.findById(getMid()).getPhone());
//    String code = cacheRepository.get(key);
//    if (StrUtil.isBlank(code) || !code.equals(memberUpdateVO.getCode())) {
//      return R.fail(code, "验证码错误");
//    }
    Member member = new Member();
    BeanUtils.copyProperties(memberUpdateVO,member);
    member.setId(getMid());
    return R.success(memberService.updateById(member));
  }



}
